package com.hznu.jspFinal.controller;


import com.hznu.jspFinal.entity.User;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "用户登录接口",value="login")
@RestController
public class LoginController {

    @Autowired
    UserService userService;


    /**
     * author: fyk
     * time: 10:34 2018/6/7
     * function: myFunction
     *
     * @return
     */
//    @ApiOperation(value="用户登录")
//    @PostMapping("/toLogin")
//    public R loignForm(@RequestParam("username") String username,@RequestParam("password") String password)
//    {
//        User user = new User();
//        user.setUsername(username);user.setPassword(password);
//        User tUser = userService.findByUsername(user.getUsername());
//        if(tUser!=null)
//        {
//            if(tUser.getPassword().equals(user.getPassword()))
//            {
//                return R.ok("登录成功").put("data",tUser);
//            }else
//            {
//                return R.error("密码错误");
//            }
//        }else
//        {
//            return R.error("用户名错误");
//        }
//    }


    @ApiOperation(value="用户登录")
    @PostMapping("/toLogin")
    public R loignForm(@RequestBody User user)
    {
        User tUser = userService.findByUsername(user.getUsername());
        if(tUser!=null)
        {
            if(tUser.getPassword().equals(user.getPassword()))
            {
                return R.ok("登录成功").put("data",tUser);
            }else
            {
                return R.error("密码错误");
            }
        }else
        {
            return R.error("用户名错误");
        }
    }


}
